﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "CombatComponent.h"

#include "Components/SkeletalMeshComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "CombatComponent.h"
#include "hyMutiplayer/Blaster/Weapon/Weapon.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "hyMutiplayer/Blaster/Character/BlasterAnimlnstance.h"
#include "hyMutiplayer/Blaster/Character/BlasterCharacter.h"

#include "CombatComponent.generated.h"

// Sets default values for this component's properties
UCombatComponent::UCombatComponent(): Character(nullptr), EquippedWeapon(nullptr), bAiming(false)
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = false;


	BaseWalkSpeed = 600.f; //基础行走速度
	AimingSpeed = 450.f; //瞄准速度
}




// Called when the game starts 游戏初始化
void UCombatComponent::BeginPlay()
{
	Super::BeginPlay();
	if (Character)
	{
		//如果角色在移动，则让最大行走速度等于基础行走速度
		Character->GetCharacterMovement()->MaxWalkSpeed = BaseWalkSpeed;
	}
}

//会达到角色倾斜头部扫射的效果，非常搞笑，混合空间进行插值
void UCombatComponent::OnRep_EquippedWeapon()
{
	//如果装备的武器 和角色有效
	if (EquipWeapon&& Character)
	{
		Character->GetCharacterMovement()->bOrientRotationToMovement = false;
		Character->bUseControllerRotationYaw = true; // 偏转控制导航
	}
}

void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{
	if(Character == nullptr||WeaponToEquip == nullptr) return;

	EquippedWeapon = WeaponToEquip;
	EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
	const USkeletalMeshSocket * HandSocket =  Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
	//const USkeletalMeshComponent * HandSocket =  Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
	 
	if (EquippedWeapon)
	{
		EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);

		const USkeletalMeshComponent* MeshComp = Character->GetMesh();
		if (MeshComp)
		{
			const USkeletalMeshSocket* HandSocket = MeshComp->GetSocketByName(FName("RightHandSocket"));
			if (HandSocket)
			{
				// 使用 AttachToComponent 方法将武器附加到手部插槽
				EquippedWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::KeepRelativeTransform, HandSocket->GetName());
			}
		}
	}
	
	//倾斜扫射
	EquippedWeapon-> SetOwner(Character);
	EquippedWeapon-> ShowPickupWidget(false);
	Character->GetCharacterMovement()->bOrientRotationToMovement = false;
	Character->bUseControllerRotationYaw = true; // 偏转控制导航

	EquippedWeapon->GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::NoCollision);

	
	
	
}

void UCombatComponent::SetAiming(bool bIsAiming)
{

	bAiming =  bIsAiming;
	ServerSetAiming(bIsAiming); //通过RPC打电话给服务端，同步状态

	if (Character)
	{
		//打开瞄准镜，利用三元运算符，瞄准是瞄准的速度，否则是正常行走的速度
		Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming? AimingSpeed: BaseWalkSpeed;
	}
	
}

void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
{
	bAiming = bIsAiming;
	if (Character)
	{
		Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimingSpeed : BaseWalkSpeed;
	}
}





// Called every frame
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	//写一个Super
	
}

void UCombatComponent::TickComponment(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTick)
{
}